package com.eduplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eduplatform.entity.School;
import com.eduplatform.mapper.SchoolMapper;
import com.eduplatform.service.SchoolService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {

    @Override
    public Page<School> listSchools(Integer page, Integer size) {
        Page<School> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<School> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(School::getCreateTime);
        return this.page(pageParam, queryWrapper);
    }

    @Override
    public Page<School> searchSchoolsByName(String name, Integer page, Integer size) {
        Page<School> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<School> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(name)) {
            queryWrapper.like(School::getName, name);
        }

        queryWrapper.orderByDesc(School::getCreateTime);
        return this.page(pageParam, queryWrapper);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<School> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(School::getId, id);
        updateWrapper.set(School::getStatus, status);
        updateWrapper.set(School::getUpdateTime, new Date());

        return this.update(updateWrapper);
    }

    @Override
    public List<School> getAllSchools() {
        LambdaQueryWrapper<School> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(School::getIsDeleted, 0); // 只获取未删除的学校
        queryWrapper.orderByAsc(School::getName); // 按名称排序
        return this.list(queryWrapper);
    }
}